/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import alg.Evaluation;
import java.util.*;
import org.jgap.*;
import util.*;
import static alg.Constants.*;
import util.Utils.*;

/**
 *
 * @author Dragan Sahpaski
 */
public class QE_Evolve {

    String log;
    String summary;

    public QE_Evolve(String testName,
            int evolutions, int populationSize,
            String domainFileName, DwConfiguration dwConf)
            throws Exception {

        String start = "Start: " + new Date();
        log = "\nEvolutions: " + evolutions + "\nPopulation size: " + populationSize + "\ndomain: " + domainFileName + "\nqueries: " + "\nallowedMaintenanceCost: " + dwConf.getAllowedMaintenanceCost() + "\nrho: " + dwConf.getRho();
        //log += Printing.printViews(domain);
        //log += Printing.printRelations(domain);
        Evaluation evaluation = new Evaluation(dwConf);
        Configuration conf = Utils.getDefaultConfiguration(populationSize, evaluation);
        Genotype population = Utils.getRandomGenotype(conf);

        log += "\n";
        long startTime = System.currentTimeMillis();
        System.out.println(MAX_VALUE);
        for (int i = 0; i < evolutions; i++) {
            population.evolve();
            String str = "\nEvolution " + (i + 1) + ": " + MAX_VALUE + " " + population.getFittestChromosome().getFitnessValue() + " " + (MAX_VALUE - population.getFittestChromosome().getFitnessValue());
            System.out.print(str);
            log += str;
        }
        long endTime = System.currentTimeMillis();
        log += "\nTotal evolution time: " + (endTime - startTime) + "ms ";

        /*List<IChromosome> chromosomes = population.getFittestChromosomes(populationSize);
        double bestQE = alg.Constants.MAX_VALUE;
        IChromosome bestChromosome = population.getFittestChromosome();

        for (IChromosome c : chromosomes) {
        c.getGene(evaluation.getDomain().getViewGraph().getRoot().getTableIndex()).setAllele(new Boolean(true));
        evaluation.getDomain().calculateTableVariants(c);
        evaluation.evaluate();
        if (evaluation.getViewMaintenanceCost() <= dwConf.getAllowedMaintenanceCost()) {
        if (evaluation.getQueryExecutionCost() < bestQE) {
        bestChromosome = c;
        bestQE = evaluation.getQueryExecutionCost();
        }
        }
        }

        if (bestChromosome == null) {
        log += "\nNO SOLUTION FOUND";
        bestChromosome = population.getFittestChromosome();
        }*/
        IChromosome bestChromosome = population.getFittestChromosome();
        bestChromosome.getGene(evaluation.getDomain().getViewGraph().getRoot().getTableIndex()).setAllele(new Boolean(true));
        evaluation.getDomain().calculateTableVariants(bestChromosome);
        evaluation.evaluate();
        Utils.saveDomain(domainFileName, dwConf.getDomain());

        summary = "queryCost:" + evaluation.getQueryExecutionCost() + " maintCost:" + evaluation.getViewMaintenanceCost() + " totalCost:" + evaluation.getTotalCost();
        log = summary + "\n" + start + "\nEnd: " + new Date() + "\n" + log;
        System.out.println("LOG:\n" + log);
    }
    private static final int MAX_ALLOWED_EVOLUTIONS = 400;
    private static final int POPULATION_SIZE = 20;
    private static final String DOMAIN_FILENAME = "domain100";

    public String getLog() {
        return log;
    }

    public String getSummary() {
        return summary;
    }

    public QE_Evolve(String testName, String domainFileName, DwConfiguration dwConf) throws Exception {
        this(testName, MAX_ALLOWED_EVOLUTIONS, POPULATION_SIZE, domainFileName, dwConf);
    }

    public QE_Evolve() throws Exception {
        this("test", MAX_ALLOWED_EVOLUTIONS, POPULATION_SIZE, DOMAIN_FILENAME, GridConf.getDwConfiguration());
    }

    public static void main(String[] args) throws Exception {
        new QE_Evolve();
    }
}







